package com.zxl.grocery.modular.crawler.controller;

import com.zxl.grocery.core.base.controller.BaseController;
import com.zxl.grocery.core.common.annotion.BussinessLog;
import com.zxl.grocery.core.common.annotion.Permission;
import com.zxl.grocery.core.common.constant.Const;
import com.zxl.grocery.core.common.constant.dictmap.CrawlerDict;
import com.zxl.grocery.core.common.constant.dictmap.DeleteDict;
import com.zxl.grocery.core.common.constant.dictmap.FunnyDict;
import com.zxl.grocery.core.common.exception.BizExceptionEnum;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.kernel.model.exception.ServiceException;
import com.zxl.grocery.modular.crawler.entity.CrawlerFunny;
import com.zxl.grocery.modular.crawler.mapper.CrawlerFunnyMapper;
import com.zxl.grocery.modular.crawler.warpper.FunnyMgrWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 笑话管理控制器
 *
 * @author zxl
 * @Date 2017-08-03 16:37:01
 */
@Controller
@RequestMapping("/funnyMgr")
public class FunnyMgrController extends BaseController {

    private String PREFIX = "/crawler/funnyMgr/";

    @Resource
    private CrawlerFunnyMapper crawlerFunnyMapper;

    /**
     * 跳转到笑话管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "funnyMgr.html";
    }

    /**
     * 跳转到添加笑话管理
     */
    @RequestMapping("/funnyMgr_add")
    public String funnyMgrAdd() {
        return PREFIX + "funnyMgr_add.html";
    }

    /**
     * 跳转到修改笑话管理
     */
    @RequestMapping("/funnyMgr_update/{funnyMgrId}")
    public String funnyMgrUpdate(@PathVariable Integer funnyMgrId, Model model) {
        if (ToolUtil.isEmpty(funnyMgrId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //System.out.println(funnyMgrId);
        CrawlerFunny crawlerFunny = crawlerFunnyMapper.selectById(funnyMgrId);
        //System.out.println(crawlerFunny.toString());
        model.addAttribute("crawlerFunny", crawlerFunny);
        LogObjectHolder.me().set(crawlerFunny);
        return PREFIX + "funnyMgr_edit.html";
    }

    /**
     * 获取笑话管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> result = crawlerFunnyMapper.getAllFunnyList();
        return super.warpObject(new FunnyMgrWarpper(result));
    }

    /**
     * 新增笑话管理
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Permission(Const.ADMIN_NAME)
    @BussinessLog(value = "新增笑话", key = "title", dict = FunnyDict.class)
    @ResponseBody
    public Object add(CrawlerFunny crawlerFunny) {
        //System.out.println(crawlerFunny.toString());
        crawlerFunny.setCreateDate(new Date());
        crawlerFunny.setAuthor(ShiroKit.getUser().getName());
        crawlerFunny.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除笑话管理
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除笑话", key = "title,funnyMgrId", dict = DeleteDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer funnyMgrId) {
        if (ToolUtil.isEmpty(funnyMgrId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.crawlerFunnyMapper.deleteById(funnyMgrId);
        return SUCCESS_TIP;
    }

    /**
     * 修改笑话管理
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改笑话", key = "title,typeId,sourcesId,placedTop,status,content,sort,label,recommend", dict = CrawlerDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(CrawlerFunny crawlerFunny) {
        if (ToolUtil.isEmpty(crawlerFunny) || crawlerFunny.getId() == null) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        int id = crawlerFunny.getId();
        CrawlerFunny temp = crawlerFunnyMapper.selectById(id);
        crawlerFunny.setAuthor(ShiroKit.getUser().getName());
        crawlerFunny.setId(temp.getId());
        crawlerFunny.setModifyDate(new Date());
        crawlerFunny.setThumbUp(temp.getThumbUp());
        crawlerFunnyMapper.updateById(crawlerFunny);
        //System.out.println(crawlerFunny.toString());
        return SUCCESS_TIP;
    }

    /**
     * 笑话管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
